package com.biyabi.config;/*
package com.baizhi.config;

import com.baizhi.util.SerializeUtil;
import com.baizhi.util.SpringUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Configuration
@Aspect
public class RedisAOPConfig {
    @Autowired
    private Jedis jedis;
    @AfterReturning("execution(* com.baizhi.service.impl.*.do*(..))")
    public void flush(){
        jedis.flushDB();
    }
    @Before("execution(* com.baizhi.service.impl.*.find*(..))")
    public void load(JoinPoint joinPoint) throws Exception {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        String argsName =  "";
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            argsName += arg.toString();
        }
        byte[] data = jedis.get((className+methodName+argsName).getBytes());
        if(data == null){
            Class clazz = joinPoint.getTarget().getClass().getDeclaredFields()[0].getType();
            Object obj = SpringUtil.getBean(clazz);
            Method method = null;
            if(args .length == 0){
                method = clazz.getDeclaredMethod(methodName, null);
                Object o = method.invoke(obj, null);
                byte[] bytes = SerializeUtil.serialize(o);
                jedis.set((className+methodName+argsName).getBytes(),bytes);
            }else{
                List<Class> classes = new ArrayList<>();
                for (Object arg : args) {
                    classes.add(arg.getClass());
                }
                Class[] classArray = new Class[args.length];
                method = clazz.getDeclaredMethod(methodName,classes.toArray(classArray));
                Object o = method.invoke(obj,args);
                byte[] bytes = SerializeUtil.serialize(o);
                jedis.set((className+methodName+argsName).getBytes(),bytes);
            }
        }
    }
}
*/
